﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using FluentValidation;
using FluentValidation.Attributes;
using PicStore.Common;

namespace PicStore.Web.Models.User
{
    [Validator(typeof(ResetPasswordModelValidator))]
    public class ResetPasswordModel
    {
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "New Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm New Password")]
        public string ConfirmPassword { get; set; }

        public int Id { get; set; }

        public bool IsValid { get; set; }
        public string ErrorMessage { get; set; }

        public static ResetPasswordModel BuildUserModel(Model.User user)
        {
            var model = new ResetPasswordModel()
            {
                Id = user.UserId,
                UserName = user.UserName,
            };

            return model;
        }
    }

    public class ResetPasswordModelValidator : AbstractValidator<ResetPasswordModel>
    {
        public ResetPasswordModelValidator()
        {
            int lengthLimit = 6;
            RuleFor(x => x.Password).NotNull().Must(x => x.Length >= lengthLimit)
                .WithMessage(string.Format(Constants.MinLength, "Password", lengthLimit));
            RuleFor(x => x.ConfirmPassword).NotNull().WithMessage(Constants.PwdMismatch);
            RuleFor(x => x.ConfirmPassword).Equal(x => x.Password).WithMessage(Constants.PwdMismatch);
        }
    }
}